package com.xyh.flowershop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyh.flowershop.annotations.RefreshToken;
import com.xyh.flowershop.constant.CommonConstant;
import com.xyh.flowershop.service.CartService;
import com.xyh.flowershop.vo.req.cart.CartEditReqVo;
import com.xyh.flowershop.vo.req.cart.CartPageReqVo;
import com.xyh.flowershop.vo.req.cart.UpdateCheckedReqVo;
import com.xyh.flowershop.vo.resp.cart.CartPageRespVo;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 小约翰可汗
 * @className CartController
 * @createTime 2024/5/420:41
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RefreshToken
@RestController
@RequestMapping("/cart/")
@Tag(name = "购物车接口")
public class CartController {

    private final CartService cartService;

    public CartController(CartService cartService) {
        this.cartService = cartService;
    }
    @Operation(description = "新增/编辑商品到购物车")
    @PostMapping("addOrEdit")
    public ResultRespVo addOrEdit(@RequestBody CartEditReqVo vo){
        return  ResultRespVo.success(cartService.addOrEditCart(vo));
    }

    @Operation(description = "分页查询")
    @PostMapping("find/page")
    public ResultRespVo findPageCarts(@RequestBody CartPageReqVo vo){
        IPage<CartPageRespVo> page = cartService.findPageCarts(vo);
        return ResultRespVo.success(page);
    }

    @Operation(description = "根据编号查找")
    @GetMapping("find/{id}")
    public ResultRespVo findCart(@PathVariable String id){
        return ResultRespVo.success(cartService.findCart(id));
    }


    @Operation(description = "删除")
    @DeleteMapping("delete/{id}")
    public ResultRespVo deletedCart(@PathVariable String id){
        List<String> ids = new ArrayList<>();
        ids.add(id);
        cartService.deleteCarts(ids);
        return ResultRespVo.success(200);
    }

    @Operation(description = "批量删除")
    @DeleteMapping("delete/batch")
    public ResultRespVo deletedBatchCart(@RequestBody List<String> ids){
        return ResultRespVo.success();
    }


    @Operation(description = "将选中商品id加入购物车缓存")
    @PostMapping("add/cache")
    public ResultRespVo addCacheCart(@RequestBody List<String> ids){
        cartService.addCacheCart(ids);
        return ResultRespVo.success(CommonConstant.SUCCESS);
    }

    @Operation(description = "获取缓存中的购物车商品信息")
    @GetMapping("find/cache/cart")
    public ResultRespVo getCacheCart(){
        return ResultRespVo.success(cartService.findCacheCarts());
    }

    @Operation(description = "更新选中状态")
    @PostMapping("update/checked")
    public ResultRespVo updateChecked(@RequestBody UpdateCheckedReqVo vo){
        cartService.updateChecked(vo);
        return ResultRespVo.success();
    }


}
